=begin
Copyright (c) 2010 Michael Peter Rosario

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=end

#!/usr/bin/env ruby
load_assembly "System.Data"
include System::Data
require 'erb'

class CodeGen
  def initialize(schema_file, table_name, template_file, ui_namespace, model_namespace)
    if schema_file == NIL
      raise ArgumentError.new("schema_file is NIL")
    end

    if table_name == NIL
      raise ArgumentError.new("table_name is NIL")
    end

    if template_file == NIL
      raise ArgumentError.new("template_file is NIL")
    end
	
	if ui_namespace == NIL
		raise ArgumentError.new("ui_namespace is required.")
	end

	if model_namespace == NIL
		raise ArgumentError.new("model_namespace is required.")
	end
	    
    if !File.exists?(schema_file)
      raise ArgumentError.new("Schema file does not exist: #{schema_file}" )
    end

    if !File.exists?(template_file)
      raise ArgumentError.new("template_file does not exist: #{template_file}" )
    end

    # read schema file....
    @data_set = System::Data::DataSet.new()
    @data_set.ReadXml(schema_file)
    
    #get table ref....
    @table = @data_set.Tables[table_name]
    @ui_namespace = ui_namespace
	@model_namespace = model_namespace
	
    #load ERB templates ....
    # http://stackoverflow.com/questions/980547/how-do-i-execute-ruby-template-files-erb-without-a-web-server-from-command-line    
    @template = ERB.new File.new(template_file).read, nil, "%"
	
    # http://code.google.com/appengine/docs/python/datastore/entitiesandmodels.html
    # Cases we need to cover...
    #class Pet(db.Model):
    #    name = db.StringProperty(required=True)
    #    type = db.StringProperty(required=True, choices=set(["cat", "dog", "bird"]))
    #    birthdate = db.DateProperty()
    #    weight_in_pounds = db.IntegerProperty()
    #    spayed_or_neutered = db.BooleanProperty()
    #    owner = db.UserProperty(required=True)
    
    # mapping from .NET types to Google app engine data types...
    @gae_prop_from_dot_net_type = {}
    @gae_prop_from_dot_net_type["System.String"] = "db.StringProperty()"
    @gae_prop_from_dot_net_type["System.Int32"] = "db.IntegerProperty()"    
    @gae_prop_from_dot_net_type["System.Single"] = "db.FloatProperty()"
	
	@jquery_validation_class_from_dot_net_type = {}
	@jquery_validation_class_from_dot_net_type["System.String"] = ""
	@jquery_validation_class_from_dot_net_type["System.Int32"] = "digits"
	@jquery_validation_class_from_dot_net_type["System.Single"] = "number"
	

	end
  
  def generate()
      return @template.result(binding)
  end
  
  def get_human_db_ref(strDbRef)
	strDbRef.capitalize.gsub("_"," ")
  end
  
  def print_tables()
    
    for table in @data_set.Tables
      print "#{table.TableName}\n"
      print "================================================================================\n"
      
      for col in table.Columns
        print "- #{col.ColumnName} -> #{col.DataType}\n"
      end

      print "\n"
    end
    
  end
  
end
